package xj.graph2d.app.prop;

import static xj.graph2d.app.resources.Resources.getResourceString;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import xj.graph2d.dialog.OptionDialogPanel;
import xj.util.gui.ColoredSquare;

public class ColorSelectionPanel 
  extends OptionDialogPanel 
  implements ActionListener, ColorGrid.ColorChangeListener, ChangeListener {

  public static Color[][] colorgrid = {
    { Color.white, Color.lightGray, Color.darkGray, Color.black,
      Color.gray, Color.blue, Color.cyan, Color.green },
    { Color.yellow, Color.orange, Color.pink, Color.red,
      Color.magenta, new Color(230, 230, 250), 
      new Color(0, 0, 128), new Color(64, 224, 208) } };

  public ColorSelectionPanel(JFrame owner, String title, Color color) {
    this(owner, title, color, color);
  }

  public ColorSelectionPanel(JFrame owner, String title, Color color, Color defaultColor) {
    super(owner, title); 
    this.color = color;
    this.defaultColor = defaultColor;

    setLayout(new BorderLayout());
    //setBorder(BorderFactory.createEtchedBorder());


    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());

    JPanel cp = new JPanel();
    Border border;
    /*
    border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 
					      " " + getResourceString("PickColor.text") + " ",
					      TitledBorder.CENTER, TitledBorder.TOP,
					      titleFont, Color.BLACK);
    */
    border = BorderFactory.createEtchedBorder();
    cp.setBorder(border);
    cp.setLayout(new BorderLayout());
    JLabel titleLabel = new JLabel(" " + getResourceString("PickColor.text") + " ");
    titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
    cp.add(titleLabel, BorderLayout.NORTH);
    colorGrid = new ColorGrid(16, 16, 4, 4, colorgrid);
			      //ColorGrid.defaultColorGrid);
    colorGrid.addColorChangeListener(this);
    cp.add(colorGrid, BorderLayout.CENTER);
    topPanel.add(cp, BorderLayout.WEST);

    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
    rightPanel.add(Box.createRigidArea(new Dimension(30, 10)));
    moreColorButton = new JButton(getResourceString("MoreColors.text"));
    moreColorButton.addActionListener(this);
    rightPanel.add(moreColorButton);
    rightPanel.add(Box.createRigidArea(new Dimension(30, 10)));
    defaultButton = new JButton(getResourceString("DefaultColor.text"));
    defaultButton.addActionListener(this);
    rightPanel.add(defaultButton);
    topPanel.add(rightPanel, BorderLayout.CENTER);

    transPanel = new TransparencyPanel();
    transPanel.addChangeListener(this);
    topPanel.add(transPanel, BorderLayout.SOUTH);

    add(topPanel, BorderLayout.NORTH);

    JPanel previewPanel = new JPanel();
    /*
    border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), 
					      " " + getResourceString("ColorPreview.text") + " ",
					      TitledBorder.CENTER, TitledBorder.TOP,
					      titleFont, Color.BLACK);
    */
    border = BorderFactory.createEtchedBorder();
    border = BorderFactory.createCompoundBorder(border, 
						BorderFactory.createEmptyBorder(5, 5, 5, 5));

    previewPanel.setBorder(border);
    colorIcon = new ColoredSquare(color, 80, 40);
    colorLabel = new JLabel(colorIcon);  
    previewPanel.setLayout(new BorderLayout());
    titleLabel = new JLabel(" " + getResourceString("ColorPreview.text") + " ");
    titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
    previewPanel.add(titleLabel, BorderLayout.NORTH);
    previewPanel.add(colorLabel, BorderLayout.CENTER);
    add(previewPanel, BorderLayout.CENTER);

    setPreferredSize(new Dimension(300, 200));

  }

  public Color getColor() {
    return color;
  }

  public void setColor(Color color) {
    colorChanged(color);
    this.color = color;
    int a = color.getAlpha();
    transPanel.setValue(a);
    colorIcon.setColor(color);
    colorLabel.repaint();
  }

  // method for ChangeListener of TransparencyPanel
  public void stateChanged(ChangeEvent e) {
    colorChanged(color);
  }

  // method for ColorGrid.ColorChangeListener
  public void colorChanged(Color c) {
    int a = transPanel.getValue();
    if (a == 255) {
      color = c;
    } else {
      color = new Color(c.getRed(), c.getGreen(), c.getBlue(), a);
    }
    colorIcon.setColor(color);
    colorLabel.repaint();
  }

  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == moreColorButton) {
      // chooser.setAlwaysOnTop(true);
      Color selectedColor = JColorChooser.showDialog(null, 
						     getResourceString("ColorChooser.title"), 
						     color);
      if (selectedColor != null) {
	colorChanged(selectedColor);
      }
    } else if (source == defaultButton) {
      transPanel.setValue(255);
      colorChanged(defaultColor);
    }
  }

  protected Color color;

  protected Color defaultColor;

  protected ColoredSquare colorIcon;

  protected JLabel colorLabel;

  protected JButton moreColorButton;

  protected JButton defaultButton;

  protected TransparencyPanel transPanel;

  protected ColorGrid colorGrid;

  protected JColorChooser chooser = new JColorChooser();

}
